package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * Created by WH on 2018/8/13.
 */
public class MyTransAndScaleView extends SurfaceView implements SurfaceHolder.Callback {
    private Context context;
    private SurfaceHolder surfaceHolder;
    private Thread thread;

    private int initLeftMargin;
    private int initTopMargin;
    private int initRightMargin;
    private int initBottomMargin;

    private int leftLength;
    private int rightLength;
    private int topLength;
    private int bottomLength;
    private int screenWidht, screenHeight;
    private int leftMargin, topMargin;
    private int initWidth, initHeight;
    private int whiteHeight;
    private int count;
    private int time = 400;
    private Handler handler;

    public MyTransAndScaleView(Context context) {
        super(context);
    }

    public MyTransAndScaleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        surfaceHolder = getHolder();
        surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
        surfaceHolder.addCallback(this);
        initLeftMargin = leftMargin;
        initTopMargin = topMargin;
        initRightMargin = (screenWidht - leftMargin - initWidth);
        initBottomMargin =  (screenHeight - topMargin - initHeight);

        leftLength = leftMargin;
        rightLength = (screenWidht * 1 / 3 - initRightMargin);
        topLength = topMargin;
        bottomLength = whiteHeight - initBottomMargin;
        handler = new Handler();
    }

    public MyTransAndScaleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void init(int leftMargin, int topMargin, int initWidth, int initHeight, boolean isHasTaste) {
        this.leftMargin = leftMargin;
        this.topMargin = topMargin;
        this.initWidth = initWidth;
        this.initHeight = initHeight;

        Log.d(TAG, "isHasTaste = " + isHasTaste);
        if (isHasTaste) {
            whiteHeight = (int) (screenHeight * 0.3);
        } else {
            whiteHeight = 0;
        }


        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
 /*       setImageBitmap(coffeeInfo.getCropImage());
        int left = leftMargin;
        int top = topMargin;
        int right = DensityUtil.getScreenWidth(mHomeActivity) - leftMargin - coffeeInfo.getMinWidth();
        int bottom = DensityUtil.getScreenHeight(mHomeActivity) - topMargin - coffeeInfo.getMinHeight();
        setScaleType(ImageView.ScaleType.CENTER_CROP);
        mCropImage.setLayoutParams(params);
        mCropImage.setPadding(left, top, right, bottom);*/

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //  canvas.drawBitmap(mBitmap, mRectSrc, mRectDes, null);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (thread == null) {
            thread = new Thread(new MyThread());
            thread.start();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            /**
             * 修改波浪的宽度
             */
            int measuredWidth = getMeasuredWidth();
            /**
             * 修改波浪的高度
             */
            int measuredHeight = getMeasuredHeight();
            /**
             * 总共平移的间隔
             */
            while (true) {

                if (count >= 400) {

                    break;
                }
                Canvas canvas = surfaceHolder.lockCanvas();

                try {
                    sleep(10);
                    canvas.drawColor(Color.BLUE);

                    int left = initLeftMargin - count * leftLength / time;
                    int right = initRightMargin + count * rightLength / time;
                    int top = initTopMargin - count * topLength / time;
                    int bottom = initBottomMargin + count * bottomLength / time;

                    if (left <= 0) {
                        left = 0;
                    }
                    if (right <= 0) {
                        right = 0;
                    }
                    if (top <= 0) {
                        top = 0;
                    }
                    if (bottom <= 0) {
                        bottom = 0;
                    }
                    count += 10;
                    final int finalLeft = left;
                    final int finalTop = top;
                    final int finalRight = right;
                    final int finalBottom = bottom;

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                                    , ViewGroup.LayoutParams.MATCH_PARENT);
                            setPadding(finalLeft, finalTop, finalRight, finalBottom);
                            setLayoutParams(params);

                        }
                    });

                    surfaceHolder.unlockCanvasAndPost(canvas);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (
                        Exception e)

                {
                    e.printStackTrace();
                } finally

                {
                    //surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

        }
    }
}
